﻿#include "MeasureView/MeasureCircle2P.h"
#include "MeasureView/UnitConvert.h"
#include "MeasureView/MeasureDataSet.h"
#include <QtMath>
namespace Qly {

MeasureCircle2P::MeasureCircle2P(MeasureDataSet *parent)
    :MeasureElement(parent)
{
    m_name = u8"直径圆";
    m_measureType = Circle2P;
    m_results_pix.resize(5);
    m_results_um.resize(5);
    m_keyPoints_pix.resize(3);//  第三个用来存圆心
    m_keyPoints_um.resize(3);
    m_measureState = DS_IDLE;
    m_textTemplate = parent->m_textTemplate_MeasureCircle2P;
}


void MeasureCircle2P::compute()
{
    double x1 = m_keyPoints_pix[0].x();
    double y1 = m_keyPoints_pix[0].y();
    double x2 = m_keyPoints_pix[1].x();
    double y2 = m_keyPoints_pix[1].y();

    m_results_pix[0] = (x1 + x2) / 2.0; //圆心 X 坐标
    m_results_pix[1] = (y1 + y2) / 2.0;
    m_results_pix[3] = hypot(x2 - x1, y2 - y1); //直径
    m_results_pix[2] = m_results_pix[3] / 2.0; // 半径
    m_results_pix[4] = M_PI * m_results_pix[2] * m_results_pix[2]; //面积

    double ps = m_pParent->m_pixSize_um;
    m_results_um[0] = ps * m_results_pix[0];
    m_results_um[1] = ps * m_results_pix[1];
    m_results_um[2] = ps * m_results_pix[2];
    m_results_um[3] = ps * m_results_pix[3];
    m_results_um[4] = ps * ps * m_results_pix[4];

    m_keyPoints_um[2] = QPointF(m_results_um[0], m_results_um[1]);
    m_keyPoints_pix[2] = QPointF(m_results_pix[0], m_results_pix[1]);
}

void MeasureCircle2P::updateText(bool updatePosition)
{
    double dia = m_results_um[3];
    double radius = m_results_um[2];

    QString text = m_textTemplate;
    m_pParent->m_unitConvert_MeasureCircle2P.replaceLength(text, dia, "@diameter", "@unit");
    m_pParent->m_unitConvert_MeasureCircle2P.replaceLength(text, radius, "@radius", "@unit");
    m_pParent->m_unitConvert_MeasureCircle2P.replaceArea(text, m_results_um[4], "@area", "@areaUnit");

    QString str3 = QString::number(m_id);
    text.replace("@id", str3, Qt::CaseSensitive);

    m_textBlock.setText(text);
    if(updatePosition)
    {
        QPointF p_pix = QPointF(m_results_pix[0], m_results_pix[1]);
        QPointF p_um = QPointF(m_results_um[0], m_results_um[1]);
        m_textBlock.moveTo(p_pix, p_um);
    }
}

int MeasureCircle2P::mouseLButtonPress(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_IDLE )
    {
        m_keyPoints_pix[0] = point_pix;
        m_keyPoints_um[0] = point_um;
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        compute();
        updateText(true);
        m_measureState = DS_STATE1;
        return m_measureState;
    }
    if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        compute();
        updateText(true);
        if(m_results_pix[2] == 0)
        {
            m_measureState = DS_ERROR;
        }
        else
        {
            if(m_textTemplate.contains("@radius"))
            {
                m_name += u8":半径";
            }
            else if(m_textTemplate.contains("@diameter"))
            {
                m_name += u8":直径";
            }
            else if(m_textTemplate.contains("@area"))
            {
                m_name += u8":面积";
            }
            m_measureState = DS_FINISH;  
        }
        return m_measureState;
    }
    return m_measureState;
}

//int MeasureCircle2P::setLens(int oldLens, int newLens, int method)
//{
//    if(oldLens == newLens || oldLens == -1) return 0;// 这时不需要更新任何数据
//    if(m_keyPoints_pix.size() != m_keyPoints_um.size()) //这里表示出现了错误，也应该退出
//    {
//        return 0;
//    }
//    LensManager & lens = LensManager::getInstance();
//    if(method == 0)//只更新长度的数值，图元在图像上的位置不变
//    {
//        double new_pixelSize = lens.currentLens().pixelSize_um;
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_um[i] = m_keyPoints_pix[i] * new_pixelSize;
//        }
//    }
//    else
//    {
//        LensData & oldData = lens.lensByIndex(oldLens);
//        LensData & newData = lens.currentLens();
//        double dx = (newData.xCompensate_um - oldData.xCompensate_um);
//        double dy = (newData.yCompensate_um - oldData.yCompensate_um);
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_pix[i] = (m_keyPoints_um[i] + QPointF(dx, dy)) / newData.pixelSize_um;
//        }
//    }
//    compute();
//    updateText();
//    return 1;
//}

void MeasureCircle2P::paint(QPainter &painter)
{
    if( m_measureState != DS_IDLE  || m_measureState != DS_ERROR)
    {
        painter.save();
        painter.setPen(m_pen);
        int x, y, width, height;
        x = m_results_pix[0] - m_results_pix[2];
        y = m_results_pix[1] - m_results_pix[2];
        width = m_results_pix[3];
        height = m_results_pix[3];
        painter.drawEllipse(x, y, width, height);

        drawCross(painter, QPointF(m_results_pix[0], m_results_pix[1]));
//        painter.drawLine(m_results_pix[0] - 5, m_results_pix[1], m_results_pix[0] + 5, m_results_pix[1]);
//        painter.drawLine(m_results_pix[0], m_results_pix[1] - 5, m_results_pix[0], m_results_pix[1] + 5);

        drawCross(painter, m_keyPoints_pix[0]);
        drawCross(painter, m_keyPoints_pix[1]);
//        painter.drawLine(m_keyPoints_pix[0] - QPointF(5, 0), m_keyPoints_pix[0] + QPointF(5, 0));
//        painter.drawLine(m_keyPoints_pix[0] - QPointF(0, 5), m_keyPoints_pix[0] + QPointF(0, 5));

//        painter.drawLine(m_keyPoints_pix[1] - QPointF(5, 0), m_keyPoints_pix[1] + QPointF(5, 0));
//        painter.drawLine(m_keyPoints_pix[1] - QPointF(0, 5), m_keyPoints_pix[1] + QPointF(0, 5));
        m_textBlock.draw(painter);
        if(m_textMoving)
        {
            QPen pen = m_pen;
            pen.setStyle(Qt::DashDotLine);
            painter.setPen(pen);
            QPointF p = m_textBlock.anchorPoint();
            painter.drawLine(p, m_keyPoints_pix[2]);
            painter.setPen(m_pen);
        }
        painter.restore();
    }
}

int MeasureCircle2P::mouseMove(QPointF point_pix, QPointF point_um)
{
    if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        compute();
        updateText(true);
        return 3;
    }
    return 0;
}

}
